package com.capgemini.scaffold.config;

import com.capgemini.scaffold.common.BusinessException;
import com.capgemini.scaffold.common.ResponseBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseBean sysException(Exception e) {
        log.error("系统异常", e);
        return ResponseBean.failed();
    }

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public ResponseBean sysException(BusinessException e) {
        log.error("业务异常", e);
        return ResponseBean.failed(e.getCode(), e.getMessage(), e.getErrorData());
    }
}
